<template>
  <div>
    <el-form label-width="4em">
      <el-form-item label="文件名">
        <el-input v-model="fileName"></el-input>
      </el-form-item>
    </el-form>
    <div :style="{textAlign:'center'}">
      <el-button type="success" @click="getExcel">导出</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fileName: "",
    };
  },
  computed: {
    source() {
      return this.$store.state.query.sourceInspect;
    },
  },
  methods: {
    getExcel() {
      let source = this.source;
      require.ensure([], () => {
        const {
          export_json_to_excel,
        } = require("@/components/Excel/Export2Excel.js");
        const tHeader = [
          "当事人",
          "班组",
          "科室",
          "起始时间",
          "提交时间",
          "检查时长",
          "席位",
          "扇区",
          "分类1",
          "分类2",
          "分类3",
          "分类备注",
          "计分",
          "详情",
          "记录人",
          "记录人班组",
          "记录人科室",
          "检查类型",
        ];
        const filterVal = [
          "clientName",
          "clientGroup",
          "clientOffice",
          "begin",
          "time",
          "duration",
          "seat",
          "sector",
          "category",
          "item",
          "subitem",
          "remark",
          "value",
          "comment",
          "userName",
          "userGroup",
          "userOffice",
          "type",
        ];
        const list = source;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, this.fileName);
        this.$notify.success({
          title: "已导出文件",
          message: this.fileName + ".xlsx",
        });
        this.$emit("close");
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) => filterVal.map((j) => v[j]));
    },
    genFileName() {
      let d = new Date();
      let str = "";
      let month = d.getMonth() + 1;
      let date = d.getDate();
      let hour = d.getHours();
      let minute = d.getMinutes();
      let second = d.getSeconds();
      str += d.getFullYear() + "";
      if (month < 10) {
        str += "0" + month + "";
      }
      if (month >= 10) {
        str += month + "";
      }
      if (date < 10) {
        str += "0" + date + "_";
      }
      if (date >= 10) {
        str += date + "_";
      }
      if (hour < 10) {
        str += "0" + hour + "";
      }
      if (hour >= 10) {
        str += hour + "";
      }
      if (minute < 10) {
        str += "0" + minute + "";
      }
      if (minute >= 10) {
        str += minute + "";
      }
      if (second < 10) {
        str += "0" + d.getSeconds();
      }
      if (second >= 10) {
        str += d.getSeconds();
      }
      return "纠违记录@" + str;
    },
  },
  mounted() {
    this.fileName = this.genFileName();
    // this.getExcel(this.res);
  },
};
</script>